package com.loyal.mwm.ext

import android.content.DialogInterface
import android.content.Intent
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.base.kit.TimeUtil
import java.util.*

fun Intent?.stringExtra(name: String): String {
    if (null == this)
        return ""
    return getStringExtra(name).replaceNull()
}

fun Intent?.boolExtra(name: String, defValue: Boolean = false): Boolean {
    if (null == this)
        return defValue
    return getBooleanExtra(name, defValue)
}

fun Long.millis2DateTime(format: String = TimeUtil.TIME_ALL): String {
    return TimeUtil.get(Date(this), format)
}

fun AppCompatActivity.alert(
    content: CharSequence?,
    nextListener: DialogInterface.OnClickListener?,
    outsideCancel: Boolean = true
) {
    alert(content, "确 定", nextListener, "", null, outsideCancel)
}

fun AppCompatActivity.alert(
    content: CharSequence?,
    nextText: String?, nextListener: DialogInterface.OnClickListener?,
    cancelText: String?, cancelListener: DialogInterface.OnClickListener?,
    outsideCancel: Boolean = true
) {
    if (isFinishing || isDestroyed) return
    val alertDialogBuilder = AlertDialog.Builder(this)
        .setTitle("温馨提示")
        .setMessage(content)
        .setCancelable(outsideCancel)
    if (null != nextListener)
        alertDialogBuilder.setPositiveButton(nextText, nextListener)
    if (null != cancelListener)
        alertDialogBuilder.setNegativeButton(cancelText, cancelListener)
    runOnUiThread { alertDialogBuilder.show() }
}
